package com.inventory.api.service;

import com.inventory.api.dao.ProductDao;
import com.inventory.api.dao.StockOrderDao;
import com.inventory.api.entity.Product;
import com.inventory.api.entity.StockOrder;
import com.inventory.api.entity.StockType;
import com.inventory.api.exception.MyException;
import com.inventory.api.signUpReq.InvntSignupReq;
import com.inventory.api.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2018/1/29.
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDao productDao;

    @Autowired
    StockTransactionService stockTransactionService;


    @Autowired
    StockOrderDao stockOrderDao;

    @Override
    @Transactional
    public Result stockOut(InvntSignupReq signupReq) {

        /**
         *  初始化出入库单据
         */
        StockOrder stockOrder = new StockOrder(signupReq, StockType.STOCK_OUT);

        /**
         * 校验出库数量
         */
        List<String> productIds = signupReq.getInventores().stream().map(e -> e.getProductId()).collect(Collectors.toList());
        Map<String, InvntSignupReq.Inventore> inventoreMap = signupReq.getInventores().stream().
                collect(Collectors.toMap(InvntSignupReq.Inventore::getProductId, product -> product));
        List<Product> products = productDao.findByIdIn(productIds);
        for (Product product : products) {
            InvntSignupReq.Inventore inventore = inventoreMap.get(product.getId());
            if (inventore == null) {
                return new Result(400, "出库失败！" + inventore.getProductName() + "库存不足！");
            }
            int ware = product.getWarehouseQuantity() - inventore.getTotal();
            if (ware < 0) {
                return new Result(400, "出库失败！" + inventore.getProductName() + "库存不足！");
            }
            product.setWarehouseQuantity(ware);
        }
        stockOrderDao.save(stockOrder);
        productDao.save(products);
        /**
         * 生成库存出库记录
         */
        stockTransactionService.addStockOut(signupReq);
        return new Result(200);
    }

    @Override
    @Transactional
    public void stockIn(InvntSignupReq signupReq) {
        /**
         *  首先保存出入库单据
         */
        StockOrder stockOrder = new StockOrder(signupReq, StockType.STOCK_IN);
        stockOrderDao.save(stockOrder);
        List<String> productIds = signupReq.getInventores().stream().map(e -> e.getProductId()).collect(Collectors.toList());
        Map<String, Product> inventoreMap = productDao.findByIdIn(productIds).stream().
                collect(Collectors.toMap(Product::getId, product -> product));
        for (InvntSignupReq.Inventore inventore : signupReq.getInventores()) {
            Product product = inventoreMap.get(inventore.getProductId());
            if (product == null) {
                product = new Product(inventore.getProductId(),
                        signupReq.getStoreId(), inventore.getProductName(),
                        inventore.getTotal());
                inventoreMap.put(product.getId(), product);
            } else {
                product.setWarehouseQuantity(product.getWarehouseQuantity() + inventore.getTotal());
            }
        }
        List<Product> products = new ArrayList<>();
        inventoreMap.forEach((k, v) -> products.add(v));
        productDao.save(products);
        /**
         * 生成库存出库记录
         */
        stockTransactionService.addStockIn(signupReq);
    }
}
